package com.jic.member.controller.manage;


import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.constant.consist.ResponseCode;
import com.jic.member.entity.TLabel;
import com.jic.member.request.TGroupLabelRelationRequest;
import com.jic.member.request.TLabelAddedRequest;
import com.jic.member.request.TLabelMemberRelationRequest;
import com.jic.member.request.TLabelRequest;
import com.jic.member.response.TLabelGradeResponse;
import com.jic.member.response.TLabelMemberListResponse;
import com.jic.member.response.TLabelResponse;
import com.jic.member.service.TLabelMemberRelationService;
import com.jic.member.service.TLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 建投数据科技(山东)有限公司
 * @date: 2020-06-04 15:47:06
 */
@RestController
@Slf4j
@RequestMapping("/tLabel")
@Api(tags = " [标签管理]控制类-PC")
public class TLabelController {
    @Autowired
    private TLabelService service;
    @Autowired
    private TLabelMemberRelationService tLabelMemberRelationService;


    /**
     * 新增记录
     *
     * @param request 新增[模板管理]请求参数
     * @return TLabelResponse 新增[模板管理]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[模板管理]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody TLabelRequest request) throws Throwable {
        TLabel tLabel = new TLabel();
        BeanUtils.copyProperties(request, tLabel);
        long flag = service.save(tLabel);
        return RestResult.success(flag);
    }

    /**
     * 新增或修改记录
     *
     * @param request 新增或修改[模板管理]请求参数
     * @return TLabelResponse 新增或修改[模板管理]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[模板管理]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody TLabelRequest request) throws Throwable {
        TLabel tLabel = new TLabel();
        BeanUtils.copyProperties(request, tLabel);
        long flag = service.saveOrUpdate(tLabel);
        return RestResult.success(flag);
    }

    /**
     * 删除记录
     *
     * @param request 删除[模板管理]请求参数
     * @return TLabelResponse 删除[模板管理]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[模板管理]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody TLabelRequest request) throws Throwable {
        TLabel tLabel = new TLabel();
        BeanUtils.copyProperties(request, tLabel);
        long flag = service.deleteByPrimaryKeyLogically(tLabel);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     *
     * @param request 修改[模板管理]请求参数
     * @return TLabelResponse 修改[模板管理]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[模板管理]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody TLabelRequest request) throws Throwable {
        TLabel tLabel = new TLabel();
        BeanUtils.copyProperties(request, tLabel);
        long flag = service.updateByPrimaryKeySelective(tLabel);
        return RestResult.success(flag);
    }

    /**
     * 单表查询详情信息
     *
     * @param request 查询[模板管理]详情请求参数
     * @return TLabelResponse 查询[模板管理]详情响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[模板管理]详情")
    @ResponseBody
    public RestResult<TLabelResponse> getByPrimaryKey(@Validated @RequestBody TLabelRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null && request.getId() != null) {

            TLabel tLabel = new TLabel();
            BeanUtils.copyProperties(request, tLabel);
            TLabel result = service.getByPrimaryKey(tLabel);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            TLabelResponse bo = new TLabelResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new TLabelResponse());
    }


    /**
     * T_LABEL 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [模板管理] ")
    @ResponseBody
    public RestResult<List<TLabelResponse>> listTLabelAllRecord(@RequestBody TLabelRequest request) throws Throwable {
        TLabel param = new TLabel();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<TLabel> list = service.listAllRecord(param);
        List<TLabelResponse> responseList = list.stream().map(temp -> {
            TLabelResponse bo = new TLabelResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageResult<TLabelResponse>> listPage(@RequestBody TLabelRequest request) throws Throwable {
        log.info("开始进入标签分页查询列表=========》,{}", request.toString());
        Page page = new Page();
        page.setLimit(request.getLimit());
        page.setStart(request.getStart());
        TLabel param = new TLabel();
        BeanUtils.copyProperties(request, param);
        //判断当前是否是母平台
        if (request.getAdminOperate().getDeptType().getCode() != 0) {
            param.setDeptId(request.getAdminOperate().getDeptId());
        }
        param.setDeleteFlag(0);
        return RestResult.success(service.listPageBySelective(param, page));
    }


    @ApiOperation(value = "获取自定义所有标签")
    @ResponseBody
    @RequestMapping("/queryCustomLabelPage")
    public RestResult<PageResult<TLabelResponse>> queryCustomLabelPage(@RequestBody TLabelRequest request) {
        log.info("开始获取自定义所有标签=========》,{}", request.toString());
        Page page = new Page();
        page.setLimit(request.getLimit());
        page.setStart(request.getStart());
        TLabel param = new TLabel();
        BeanUtils.copyProperties(request, param);
        return RestResult.success(service.queryCustomLabelPage(param, page));
    }


    /**
     * 子平台入驻默认分配母平台全部标签
     *
     * @param deptId 子平台入驻默认分配母平台全部标签
     * @return 子平台入驻默认分配母平台全部标签
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "子平台入驻默认分配母平台全部标签")
    @PostMapping("/saveLabelForSubPlatformEntry")
    @ResponseBody
    public RestResult saveLabelForSubPlatformEntry(Long deptId) throws Throwable {
        return service.saveLabelForSubPlatformEntry(deptId);
    }


    /**
     * 会员等级值新增修改
     *
     * @param map 会员等级值新增修改
     * @return 会员等级值新增修改
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "会员等级值新增修改")
    @PostMapping("/saveLabelMemberGradeValue")
    @ResponseBody
    public RestResult saveLabelMemberGradeValue(Map<String, Object> map) throws Throwable {
        return RestResult.success(service.saveLabelMemberGradeValue(map));
    }


    /**
     * 根据部门id 查询标签
     *
     * @param tGroupLabelRelationRequest 根据部门id 查询标签
     * @return 根据部门id 查询标签
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "根据部门id 查询一二级标签")
    @PostMapping("/queryLabelByDeptId")
    @ResponseBody
    public RestResult<List<TLabelGradeResponse>> queryLabelByDeptId(@RequestBody TGroupLabelRelationRequest tGroupLabelRelationRequest) throws Throwable {
        return service.queryLabelByDeptId(tGroupLabelRelationRequest);
    }


    /**
     * 根据部门id 查询标签
     *
     * @param tGroupLabelRelationRequest 根据部门id 查询标签
     * @return 根据部门id 查询标签
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "查询用户组的二三级标签")
    @PostMapping("/queryGroupLabelByDeptId")
    @ResponseBody
    public RestResult<List<TLabelGradeResponse>> queryGroupLabelByDeptId(@RequestBody TGroupLabelRelationRequest tGroupLabelRelationRequest) throws Throwable {
        return service.queryGroupLabelByDeptId(tGroupLabelRelationRequest);
    }


    /**
     * 查询部门的所有标签数据
     *
     * @return author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "查询部门的所有标签数据")
    @PostMapping("/queryAllLabelByDeptId")
    @ResponseBody
    public RestResult<PageResult<TLabelResponse>> queryAllLabelByDeptId(@RequestBody TLabelRequest request) throws Throwable {
        log.info("查询部门的所有标签数据=========》,{}", request.toString());
        Page page = new Page();
        page.setLimit(request.getLimit());
        page.setStart(request.getStart());
        TLabel param = new TLabel();
        BeanUtils.copyProperties(request, param);
        param.setDeptId(request.getAdminOperate().getDeptId());
        return RestResult.success(service.queryAllLabelByDeptId(param, page));
    }


    /**
     * 查询已经添加的标签
     *
     * @return author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "查询已经添加的标签")
    @PostMapping("/queryAddedLabel")
    @ResponseBody
    public RestResult<PageResult<TLabelResponse>> queryAddedLabel(@RequestBody TLabelAddedRequest request) throws Throwable {
        log.info("查询部门的所有标签数据=========》,{}", request.toString());
        Page page = new Page();
        page.setLimit(request.getLimit());
        page.setStart(request.getStart());
        return RestResult.success(service.queryAddedLabel(request, page));
    }

    @ApiOperation(value = "查询会员标签")
    @PostMapping("/queryLabelByThirdUserId")
    @ResponseBody
    public RestResult<List<TLabelMemberListResponse>> queryLabelByThirdUserId(@RequestBody TLabelMemberRelationRequest
                                                                                      request) throws Throwable {
        log.info("查询部门的所有标签数据=========》,{}", request.toString());
        return tLabelMemberRelationService.queryLabelByThirdUserId(request);
    }
}

